//
//  YKEmotionTextView.m
//  youkan
//
//  Created by 吴高烽 on 16/5/7.
//  Copyright © 2016年 Honsintech. All rights reserved.
//

#import "YKEmotionTextView.h"
#import "YKEmotions.h"
@implementation YKEmotionTextView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self setUpAllChilen];
    }
    return self;
}
-(void)setUpAllChilen{
    
}
-(void)layoutSubviews{
    [super layoutSubviews];
    
}
-(void)insertEmotion:(YKEmotions *)emotion{
    if (emotion.png) {
        //        [self.textView insertText:emotion.chs];
        NSMutableAttributedString *atteribtedText=[[NSMutableAttributedString alloc]init];
        //        拼接之前的文字
        [atteribtedText appendAttributedString:self.attributedText];
        //        加载图片
        NSTextAttachment *attch =[[NSTextAttachment alloc]init];
        attch.image =[UIImage imageNamed:emotion.png];
        CGFloat attchWH =self.font.lineHeight ;
        attch.bounds =CGRectMake(0, -3, attchWH, attchWH) ;
        NSAttributedString *imageStr=[NSAttributedString attributedStringWithAttachment:attch];
        //        拼接图片
        NSUInteger loc =self.selectedRange.location ;
        [atteribtedText insertAttributedString:imageStr atIndex:loc];
        //        [atteribtedText appendAttributedString:imageStr];
        [atteribtedText addAttribute:NSFontAttributeName value:self.font range:NSMakeRange(0, atteribtedText.length)];
        
        self.attributedText =atteribtedText ;
        self.selectedRange = NSMakeRange(loc + 1, 0);
    }else if (emotion.code){
        //        insertText: 将文字插入到光标位置
        [self insertText:emotion.code.emoji];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
